package tourist.springboot.web;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import tourist.springboot.bean.ResponseData;
import tourist.springboot.domain.Foods;
import tourist.springboot.domain.TouristRoute;
import tourist.springboot.service.TouristRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import tourist.springboot.utils.IDGenerateUtils;
import tourist.springboot.utils.ResponseDataUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @author wangzg
 * 旅游攻略控制器
 */
@RestController
public class TouristRouteController {

    @Autowired
    private TouristRouteService touristRouteService;

    /**
     * 旅游攻略列表
     * @return
     */
    @ApiOperation(value = "查询 旅游攻略列表", notes = "查询 旅游攻略列表")
    @GetMapping("/tourist_route/list")
    public List<TouristRoute> touristRouteList(){
        return touristRouteService.findAll();
    }

    /**
     * 查看旅游攻略详细信息
     * @param id
     * @return
     */
    @GetMapping("/tourist_route/{id}")
    public ResponseData getTouristRouteById(@PathVariable("id") String id){
        if(StringUtils.isNotEmpty(id)){
            return ResponseDataUtils.successResponse(touristRouteService.findById(id));
        }
        return ResponseDataUtils.errorResponse();
    }

    /**
     * 新增旅游攻略信息
     * @param touristRoute
     * @return
     */
    @PostMapping("/tourist_route")
    public ResponseData addTouristRoute(@RequestBody TouristRoute touristRoute){
        if(null != touristRoute){
            touristRoute.setId(IDGenerateUtils.generateId());
            touristRouteService.insert(touristRoute);
            return ResponseDataUtils.successResponse();
        }
        return ResponseDataUtils.errorResponse();
    }

    /**
     * 修改旅游攻略信息
     * @param touristRoute
     * @return
     */
    @PutMapping("/tourist_route")
    public ResponseData updateTouristRoute(@RequestBody TouristRoute touristRoute){
        if(null != touristRoute){
            touristRouteService.update(touristRoute);
            return ResponseDataUtils.successResponse();
        }
        return ResponseDataUtils.errorResponse();
    }

    /**
     * 删除旅游攻略
     * @param id
     * @return
     */
    @DeleteMapping("/tourist_route/{id}")
    public ResponseData deleteTouristRoute(@PathVariable("id") String id){
        if(StringUtils.isNotEmpty(id)){
            touristRouteService.delete(id);
            return ResponseDataUtils.successResponse();
        }
        return ResponseDataUtils.errorResponse();
    }

    /**
     * 获取旅游攻略相关推荐
     * @param id
     * @return
     */
    @GetMapping("/tourist_route/{id}/recommend")
    public ResponseData getRecommendListByTouristRoute(@PathVariable("id") String id){
        if(StringUtils.isNotEmpty(id)){
            TouristRoute touristRoute = touristRouteService.findById(id);
            if(null != touristRoute){
                String recommendIdStr = touristRoute.getRecommend();
                if(StringUtils.isNotEmpty(recommendIdStr)){
                    List<TouristRoute> recommendList = touristRouteService.findByIds(Arrays.asList(recommendIdStr.split(",")));
                    return ResponseDataUtils.successResponse(recommendList);
                }
            }

        }
        return ResponseDataUtils.errorResponse();
    }
}
